package com.hq.base.web;

import com.hq.base.service.BaseService;
import com.hq.base.web.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 统一的异常拦截，以JSON的格式返回

 * @since 2015-02-01 16:14
 */
@Controller
public class ErrorController extends BaseController {

    @RequestMapping(value = "/error", produces = "application/json")
    @ResponseBody
    public Map<String, Object> handle(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", request.getAttribute("javax.servlet.error.status_code"));
        Map<String, Object> message = new HashMap<String, Object>();
        message.put("uri", request.getAttribute("javax.servlet.error.request_uri"));
        //message.put("mediaTypes", request.getAttribute("org.springframework.web.servlet.HandlerMapping.producibleMediaTypes").toString());
        message.put("reason", request.getAttribute("javax.servlet.error.message"));
        map.put("message", message);
        map.put("data", null);
        logger.error(message.get("reason").toString());
        return map;
    }

    @Override
    public BaseService getService() {
        return null;
    }
}
